<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AdminWxMaterialImagePost;
use Illuminate\Http\Request;
use App\Services\WxMaterial\WxMaterialService;
/**
 * 微信图片素材管理
 * @package App\Http\Controllers\Admin
 */
class WxMaterialImageController extends BaseWxController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 素材首页列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request, WxMaterialService $wxMaterialSer)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxMaterial::with('category')->select('*');
            //当前选择微信应用数据
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //图文素材分类
            $query->whereIn('type', [WxMaterialService::TYPE_IMAGE, WxMaterialService::TYPE_THUMB]);
            //分类
            $catId = $request->get('cat_id', 0);
            if ($catId){
                $query->where('cat_id', $catId);
            }
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }
            $query->orderBy('updated_at', 'DESC');
            return $query->paginate($pageSize);
        }
        //树结构菜单列表
        $catList = $wxMaterialSer->getCatList(WxMaterialService::TYPE_IMAGE);
        $tree = new \App\Services\Tool\TreeService('cat_id', 'parent_id', 'children');
        $tree->load($catList);
        $treeMenuList = $tree->deepTree();
        $this->viewData['catList'] = $treeMenuList ? $treeMenuList : [];
        return view('admin.wxmaterialimage.index', $this->viewData);
    }

    /**
     * 保存素材数据
     * @param AdminWxMaterialImagePost $request
     * @return \Illuminate\Http\Response
     */
    public function save(AdminWxMaterialImagePost $request, WxMaterialService $wxMaterialSer)
    {
        if ($request->ajax()) {
            try {
                $wxMaterialSer->createMaterial(WxMaterialService::TYPE_IMAGE)->save($request->toArray());
                return $this->showMsg('success', '保存成功', [], 'admin.wxmaterialimage.index');
            } catch (\Exception $e) {
                return $this->showMsg('error', '保存失败:' . $e->getMessage());
            }
        }
    }

    /**
     * 素材删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request, WxMaterialService $wxMaterialSer)
    {
        if ($request->ajax()) {
            try {
                $materialId = $request->get('id', 0);
                if (empty($materialId)){
                    throw new \Exception('请选择要删除的素材');
                }
                $wxMaterialSer->createMaterial(WxMaterialService::TYPE_IMAGE)->del($materialId);
                return $this->showMsg('success', '删除成功', [], 'admin.wxmaterialimage.index');
            } catch (\Exception $e) {
                return $this->showMsg('error', '删除失败:' . $e->getMessage());
            }
        }
    }
    /**
     * 上传素材到微信端
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function upwx(Request $request)
    {
        if ($request->ajax()) {
            try {
                $materialId = $request->get('id', 0);
                if (empty($materialId)){
                    throw new \Exception('请选择要上传的素材');
                }
                //当前选择微信应用数据
                $curWxMp = \Auth::user()->getWxMpCurrent();
                $appid = $curWxMp ? $curWxMp['appid'] : '';
                \App\Jobs\WxMaterialImageUpload::dispatch($appid, $materialId)->onQueue('low');
                return $this->showMsg('success', '推送成功', [], 'admin.wxmaterialimage.index');
            } catch (\Exception $e) {
                return $this->showMsg('error', '推送失败:' . $e->getMessage());
            }
        }
    }

    /**
     * 同步拉取微信端素材
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function sync(Request $request)
    {
        if ($request->ajax()) {
            try {
                //当前选择微信应用数据
                $curWxMp = \Auth::user()->getWxMpCurrent();
                $appid = $curWxMp ? $curWxMp['appid'] : '';
                \App\Jobs\WxMaterialImageDownload::dispatch($appid)->onQueue('low');
                return $this->showMsg('success', '提交拉取任务成功', [], 'admin.wxmaterialimage.index');
            } catch (\Exception $e) {
                return $this->showMsg('error', '提交拉取任务失败:' . $e->getMessage());
            }
        }
    }

    /**
     * 获取选择数据
     * @param Request $request
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getChoice(Request $request, WxMaterialService $wxMaterialSer)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            //当前选择微信应用数据
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';

            $query = \App\Models\WxMaterial::with('category')->select('*');

            $query->where('appid', $appid);
            //判断素材ID存在
            $query->where('media_id', '<>', '');
            //图文素材分类
            $query->whereIn('type', [WxMaterialService::TYPE_IMAGE, WxMaterialService::TYPE_THUMB]);
            //分类
            $catId = $request->get('cat_id', 0);
            if ($catId){
                $query->where('cat_id', $catId);
            }
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }
            $query->orderBy('updated_at', 'DESC');
            $data = $query->paginate($pageSize)->toArray();
            //树结构菜单列表
            $catList = $wxMaterialSer->getCatList(WxMaterialService::TYPE_IMAGE);
            $tree = new \App\Services\Tool\TreeService('cat_id', 'parent_id', 'children');
            $tree->load($catList);
            $treeMenuList = $tree->deepTree();
            $data['catList'] = $treeMenuList ? $treeMenuList : [];
            return $data;
        }
    }
}
